package category;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import objects.CategoryItem;

import connect.MyConnect;

public class CategoryControl {
	private MyConnect mc;
	private Connection con;
	public CategoryControl(){
		mc = new MyConnect();
		this.con = getConnect();
	}
	public Connection getConnect(){
		return mc.getConnect();
	}
	
	//lay toan bo category
	public ArrayList getCategories(){
		String sql = " SELECT * FROM tblcategory ";
		ArrayList list = new ArrayList();
		CategoryItem cate = null;
		try {
			PreparedStatement pre = this.con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if(rs!=null){
				while(rs.next()){
					cate = new CategoryItem();
					cate.setCategory_name(rs.getString("category_name"));
					cate.setCategory_id(rs.getShort("category_id"));
					cate.setCategory_section_id(rs.getShort("category_section_id"));
					cate.setCategory_notes(rs.getString("category_notes"));
					cate.setCategory_image(rs.getString("category_image"));
					
					list.add(cate);
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//lay category theo id
	public CategoryItem getCateItem(short id){
		String sql = "SELECT * FROM tblcategory WHERE category_id = "+id;
		CategoryItem cate = null;
		try {
			PreparedStatement pre = this.con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if(rs!=null){
				while(rs.next()){
					cate = new CategoryItem();
					cate.setCategory_name(rs.getString("category_name"));
					cate.setCategory_id(rs.getShort("category_id"));
					cate.setCategory_section_id(rs.getShort("category_section_id"));
					cate.setCategory_notes(rs.getString("category_notes"));
					cate.setCategory_image(rs.getString("category_image"));
					
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cate;
	}
	
}
